import numpy as np
from scipy.spatial.distance import cdist

# 生成高维向量数据
def generate_high_dim_vectors(num_points=500, dimensions=50):
    """生成随机高维向量"""
    np.random.seed(42)  # 确保结果可重复
    data = np.random.rand(num_points, dimensions)
    return data

# 生成50维随机向量数据
high_dim_vectors = generate_high_dim_vectors(num_points=500, dimensions=50)

# 最近邻距离分析
pairwise_distances = cdist(high_dim_vectors, high_dim_vectors, metric='euclidean')
# 忽略自身的距离（设置为无穷大）
np.fill_diagonal(pairwise_distances, np.inf)
# 计算每个点的最近邻距离
nearest_distances = np.min(pairwise_distances, axis=1)

# 平均最近邻距离和方差
mean_nearest_distance = np.mean(nearest_distances)
variance_nearest_distance = np.var(nearest_distances)

print("最近邻距离的均值:", mean_nearest_distance)
print("最近邻距离的方差:", variance_nearest_distance)

# 距离分布变异系数
mean_distance = np.mean(pairwise_distances[np.isfinite(pairwise_distances)])
std_distance = np.std(pairwise_distances[np.isfinite(pairwise_distances)])
distance_variation_ratio = std_distance / mean_distance

print("\n距离分布的变异系数:", distance_variation_ratio)

# 空间覆盖率测量
# 简单的覆盖率定义为点与点间距离小于一定阈值的比例
threshold = mean_nearest_distance * 1.5
coverage = np.mean(pairwise_distances[np.isfinite(pairwise_distances)] < threshold)

print("\n空间覆盖率（阈值1.5倍最近邻距离）:", coverage)